#include "datareader.h"
#include "ui_datareader.h"
#include <iostream>
#include "chartshow.h"
#include<vector>

using namespace std;

/*class chartButton{
    void on_addButton_clicked(int stepEn);
};*/
DataReader::DataReader(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DataReader)
{
    ui->setupUi(this);
    setWindowTitle(QString("选择文件"));//设置窗口的名称
    ui->DrawLineButton->setDisabled(1);
    QTableWidget *tableWidget;
    tableWidget=new QTableWidget(this);
    tableWidget->move(125, 21);
    tableWidget->setGeometry(125, 21, 881, 581);
    tableWidget->show();
    dataReaderStaList.bitNum=16;
    ui->xTextEdit->append(QString("0"));
    ui->yTextEdit->append(QString("2"));
}

DataReader::~DataReader()
{
    delete ui;
}

void DataReader::on_OpenFileButton_clicked()
{

   fileName = QFileDialog::getOpenFileName(
                   this, tr("open image file"),
                   "./", tr("Image files(*.csv)"));

   QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))

    {
        QMessageBox msg;
        msg.setText("打开文件失败!");
        msg.exec();
    }else{
    bool isOK;
    ui->DrawLineButton->setDisabled(0);
    tableWidget=new QTableWidget(this);
    tableWidget->clear();
    tableWidget->move(125, 21);
    tableWidget->setGeometry(125, 21, 881, 581);
    tableWidget->setColumnCount(10);
    tableWidget->setRowCount(20000);
    tableWidget->show();
      QTextStream  in(&file);
      QString line,linesStr;
      QStringList fields;
      int row=0;int column = 0;
      while(!in.atEnd()){
          line = in.readLine();
          linesStr.push_back(line);
          fields = line.split(',');
          for(column = 0; column<fields.size();column++){
              tableWidget->setItem(row, column, new QTableWidgetItem(fields[column]));
          }
          row++;
      }
      dataReaderStaList.row=row;dataReaderStaList.column=column;
      //tableWidget->setColumnCount(dataReaderStaList.column);
      //tableWidget->setRowCount(dataReaderStaList.row);
      QStringList headers;
      headers << QStringLiteral("0") << QStringLiteral("1") << QStringLiteral("2") << QStringLiteral("3") << QStringLiteral("4")
              << QStringLiteral("5") << QStringLiteral("6") << QStringLiteral("7") << QStringLiteral("8") << QStringLiteral("9") ;
      tableWidget->setHorizontalHeaderLabels(headers);
      tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
      tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
      tableWidget->resizeColumnToContents(10);
      tableWidget->update();
      file.close();
      QString text = QInputDialog::getText(NULL, "New data:",
                                                                    "Resolution(bit):",
                                                                    QLineEdit::Normal,
                                                                    "16",
                                                                    &isOK);
      dataReaderStaList.bitNum=text.toInt();
    }
}

void DataReader:: on_addButton_clicked()
{
    dataReaderStaList.step+=100;
    if(dataReaderStaList.step>=1500) dataReaderStaList.step=1500;
    QVector <double> chart_x(500+dataReaderStaList.step), chart_y(500+dataReaderStaList.step);
    for(int i=0;i<500;i++)
    {
        QString str_x = tableWidget->item(i+dataReaderStaList.step,dataReaderStaList.xColumnShow)->text();//取出字符串
        QString str_y = tableWidget->item(i+dataReaderStaList.step,dataReaderStaList.yColumnShow)->text();//取出字符串
        double num_x=str_x.toDouble();
        int num_y_int=str_y.toInt();
        double num_y=(num_y_int>>(dataReaderStaList.bitNum-1))&1 ? str_y.toDouble()-(1<<dataReaderStaList.bitNum) : str_y.toDouble();
        chart_x[i]=num_x;
        chart_y[i]=num_y;
    }
    customPlot->graph(0)->addData(chart_x,chart_y);
}

void DataReader:: on_cutButton_clicked()
{

    dataReaderStaList.step-=100;
    if(dataReaderStaList.step<=0)dataReaderStaList.step=0;
    QVector <double> chart_x(500+dataReaderStaList.step), chart_y(500+dataReaderStaList.step);
    for(int i=0;i<500+dataReaderStaList.step;i++)
    {
        QString str_x = tableWidget->item(i,dataReaderStaList.xColumnShow)->text();//取出字符串
        QString str_y = tableWidget->item(i,dataReaderStaList.yColumnShow)->text();//取出字符串
        double num_x=str_x.toDouble();
        int num_y_int=str_y.toInt();
        double num_y=(num_y_int>>(dataReaderStaList.bitNum-1))&1 ? str_y.toDouble()-(1<<dataReaderStaList.bitNum) : str_y.toDouble();
        chart_x[i]=num_x;
        chart_y[i]=num_y;
    }
    customPlot->graph(0)->setData(chart_x,chart_y);
}
void DataReader::on_DrawLineButton_clicked(bool sta)
{
    static int picId=0;picId++;
    bool isOK;
    QString text = QInputDialog::getText(NULL, "Set Name:",
                                                                  "name:",
                                                                  QLineEdit::Normal,
                                                                  "",
                                                                  &isOK);
    if(text.isNull())
    {
      QString text1= fileName;
      text=text1;
    }
    if(dataReaderStaList.pointAdd<2)
    dataReaderStaList.pointAdd++;
    window= new QWidget;
    if(dataReaderStaList.pointAdd>=2) {dataReaderStaList.pointAdd=2;windowLast->close();}
    windowLast=window;
    //QVBoxLayout *layout = new QVBoxLayout;
    QList<QCPAxis*> axes;
    dataReaderStaList.xColumnShow=ui->xTextEdit->toPlainText().toInt();
    dataReaderStaList.yColumnShow=ui->yTextEdit->toPlainText().toInt();
    QVector <double> chart_x(500), chart_y(500);
    //chartshow *show_chart = new chartshow();
    //show_chart->show();
    for(int i=0;i<500;i++)
    {
        QString str_x = tableWidget->item(i,dataReaderStaList.xColumnShow)->text();//取出字符串
        QString str_y = tableWidget->item(i,dataReaderStaList.yColumnShow)->text();//取出字符串
        double num_x=str_x.toDouble();
        int num_y_int=str_y.toInt();
        double num_y=(num_y_int>>(dataReaderStaList.bitNum-1))&1 ? str_y.toDouble()-(1<<dataReaderStaList.bitNum) : str_y.toDouble();
        chart_x[i]=num_x;
        chart_y[i]=num_y;
    }
    QPen pen;
    pen.setWidth(3);//线宽
    pen.setColor(Qt::green);//黄色
    customPlot=new QCustomPlot(window);
    //static int staLast;
    //if(sta!=staLast) qDebug()<<sta<<staLast,customPlot_last->close();
    //sta=staLast;
    customPlot->addGraph()->setName(QString::number(picId));
    QCPTextElement *m_title;
    customPlot->plotLayout()->insertRow(0);
    m_title = new QCPTextElement(customPlot,text+" ID:"+QString::number(picId));
    customPlot->plotLayout()->addElement(0, 0, m_title);
    customPlot->graph(0)->setData(chart_x,chart_y);
    customPlot->graph(0)->setPen(pen);
    customPlot->xAxis->setLabel("X");
    customPlot->yAxis->setLabel("y");
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom );//| QCP::iRangeZoom QCP::iRangeDrag
    axes <<  customPlot->yAxis << customPlot->xAxis;
    customPlot->axisRect()->setRangeZoomAxes(axes);
    customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
    customPlot->axisRect()->setBackground(QBrush(Qt::black));//背景黑色
    customPlot->rescaleAxes();
    //layout->addWidget(customPlot);
    //window->setLayout(layout);
    addButton =new QPushButton(customPlot);
    QPushButton *cutButton =new QPushButton(customPlot);

    window->setGeometry(600, 100, 800, 600);
    window->show();

    customPlot->show();
    customPlot->move(0,0);
    customPlot->setGeometry(0,0,800,600);

    addButton->show();
    addButton->move(400,580);
    addButton->setGeometry(400,580,400,20);
    addButton->setText(QString("+100"));

    cutButton->show();
    cutButton->move(0,580);
    cutButton->setGeometry(0,580,400,20);
    cutButton->setText(QString("-100"));
    dataReaderStaList.step=0;
    connect(addButton,SIGNAL(clicked()), this,SLOT(on_addButton_clicked()));
    connect(cutButton,SIGNAL(clicked()), this,SLOT(on_cutButton_clicked()));
    //addButton->setDisabled(1);
    //cutButton->setDisabled(1);
}

void DataReader::on_unsightCheckBox_clicked()
{
    dataReaderStaList.unsightEn ^= 1;
    //on_DrawLineButton_clicked(dataReaderStaList.unsightEn);
}
